package ${package.Controller};

import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.sinothk.gpserver.util.DateUtils;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};

        <#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
        <#else>
import org.springframework.stereotype.Controller;
        </#if>
        <#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 *  1. ${table.comment!}
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Api(tags = "${table.comment!}")
@RequestMapping("/${table.entityPath?replace("Entity","")}")
@ResponseResult()
        <#if restControllerStyle>
@RestController
        <#else>
@Controller
</#if>
        <#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
        <#else>
        <#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
        <#else>

public class ${table.controllerName} {
</#if>

    @Resource
    private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<${entity}> page(@RequestBody PageVo<${entity}> pageVo) {
        return ${(table.serviceName?substring(1))?uncap_first}.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public ${entity} get(@ApiParam("id") @PathVariable String id) {
        return ${(table.serviceName?substring(1))?uncap_first}.getById(id);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public String add(@RequestBody ${entity} ${table.entityPath}) {
        ${table.entityPath}.setCreateBy(login.getAccount());
        ${(table.serviceName?substring(1))?uncap_first}.save(${table.entityPath});
        return "新增成功";
    }


    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public ${entity} update(@RequestBody ${entity} ${table.entityPath}) {
        if (StringUtil.isEmpty(${table.entityPath}.getId())) {
            throw new BizException("id不能为空");
        }
        ${table.entityPath}.setUpdateBy(login.getUserName());
        ${(table.serviceName?substring(1))?uncap_first}.updateById(${table.entityPath});
        return ${(table.serviceName?substring(1))?uncap_first}.getById(${table.entityPath}.getId());
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody ${entity} ${table.entityPath}) {
        if (StringUtil.isEmpty(${table.entityPath}.getId())) {
            ${table.entityPath}.setCreateBy(login.getAccount());
            ${(table.serviceName?substring(1))?uncap_first}.save(${table.entityPath});
            return "新增成功";
        } else {
            ${table.entityPath}.setUpdateBy(login.getUserName());
            ${(table.serviceName?substring(1))?uncap_first}.updateById(${table.entityPath});
            return "更新成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        ${(table.serviceName?substring(1))?uncap_first}.removeById(id);
        return "已逻辑删除";
    }
}
</#if>